package com.ruoyi.app.controller.common.service.impl;

import com.ruoyi.app.controller.common.mapper.IDistrictsMapper;
import com.ruoyi.app.controller.common.service.IDistrictsService;
import com.ruoyi.common.entity.VO.DistrictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DistrictsServiceImpl implements IDistrictsService {

    @Autowired
    private IDistrictsMapper IDistrictsMapper;

    @Override
    public List<DistrictVO> listDistricts() {
        List<DistrictVO> list = IDistrictsMapper.listDistricts();
        for (DistrictVO province : list) {
            List<DistrictVO> cityList = getClassificationMap(province.getId());
            if (cityList.size() > 0) {
                for (DistrictVO city : cityList) {
                    city.setList(getClassificationMap(city.getId()));
                }
            }
            province.setList(cityList);
        }
        return list;
    }

    private List<DistrictVO> getClassificationMap(Long parentId) {
        return IDistrictsMapper.listDistrictsByParentId(parentId);
    }
}
